package com.ruoyi.project.erp.member.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.erp.member.domain.ErpMember;
import com.ruoyi.project.erp.member.service.IErpMemberService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 会员Controller
 *
 * @author ruoyi
 * @date 2021-08-08
 */
@Controller
@RequestMapping("/erp/member")
public class ErpMemberController extends BaseController {
    private String prefix = "erp/member";

    @Autowired
    private IErpMemberService erpMemberService;

    @RequiresPermissions("erp:member:view")
    @GetMapping()
    public String member() {
        return prefix + "/member";
    }

    /**
     * 查询会员列表
     */
    @RequiresPermissions("erp:member:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ErpMember erpMember) {
        startPage();
        List<ErpMember> list = erpMemberService.selectErpMemberList(erpMember);
        return getDataTable(list);
    }

    /**
     * 导出会员列表
     */
    @RequiresPermissions("erp:member:export")
    @Log(title = "会员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ErpMember erpMember) {
        List<ErpMember> list = erpMemberService.selectErpMemberList(erpMember);
        ExcelUtil<ErpMember> util = new ExcelUtil<ErpMember>(ErpMember.class);
        return util.exportExcel(list, "会员数据");
    }

    /**
     * 新增会员
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存会员
     */
    @RequiresPermissions("erp:member:add")
    @Log(title = "会员", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ErpMember erpMember) {
        return toAjax(erpMemberService.insertErpMember(erpMember));
    }

    /**
     * 修改会员
     */
    @GetMapping("/edit/{memberOpenId}")
    public String edit(@PathVariable("memberOpenId") String memberOpenId, ModelMap mmap) {
        ErpMember erpMember = erpMemberService.selectErpMemberByMemberOpenId(memberOpenId);
        mmap.put("erpMember", erpMember);
        return prefix + "/edit";
    }

    /**
     * 修改保存会员
     */
    @RequiresPermissions("erp:member:edit")
    @Log(title = "会员", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ErpMember erpMember) {
        return toAjax(erpMemberService.updateErpMember(erpMember));
    }

    /**
     * 删除会员
     */
    @RequiresPermissions("erp:member:remove")
    @Log(title = "会员", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(erpMemberService.deleteErpMemberByMemberOpenIds(ids));
    }
}
